package com.frontop.bigdata.modules.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.frontop.bigdata.modules.service.BigDataAccessPageService;
import com.frontop.bigdata.modules.service.BigDataOverviewService;
import com.frontop.common.domain.UserJurisdiction;
import com.frontop.common.util.JwtUtil;
import com.frontop.common.util.ResultJsonUtil;
import com.frontop.dubbo.api.auth.Oauth2Api;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author YangBoos
 * @title: StatisticsController
 * @projectName frontop-cloud
 * @description: TODO
 * @date 2022/2/17 9:41
 */
@RestController
@RequestMapping("/big-data-statistics")
public class StatisticsController {

    @Autowired
    BigDataOverviewService bigDataOverviewService;

    @Autowired
    BigDataAccessPageService bigDataAccessPageService;

    @ApiOperation(value = "获取仅七天网点浏览量统计数据")
    @GetMapping("/getSevenDaysOverview/{day}")
    public ResultJsonUtil<Object> updateSite(@PathVariable("day") Integer day,
                                             Authentication authentication,
                                             HttpServletRequest request) {
        UserJurisdiction  userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        List<JSONObject> sevenDayData = bigDataOverviewService.getSevenDaysOverview(userJurisdiction.getOrganizationId(),day-1);
        JSONObject data = new JSONObject();
        List<String> date = new ArrayList<>();
        List<Integer> pvCounts = new ArrayList<>();
        List<Integer> visitorCount = new ArrayList<>();
        for(int i = 0;i<sevenDayData.size();i++){
            date.add(i,sevenDayData.get(i).getString("date"));
            pvCounts.add(i,sevenDayData.get(i).getInteger("pv_count"));
            visitorCount.add(i,sevenDayData.get(i).getInteger("visitor_count"));
        }
        data.put("dates",date);
        data.put("values",pvCounts);
        data.put("visitorCounts",visitorCount);
        return new ResultJsonUtil().success(data);
    }


    @ApiOperation(value = "获取仅网点浏览量统计数据")
    @GetMapping("/getOverviewTotalData/{day}")
    public ResultJsonUtil<Object> getOverviewTotalData(
                                            @PathVariable("day") Integer day,
                                             Authentication authentication,
                                             HttpServletRequest request) {
        UserJurisdiction  userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        JSONObject resultData = bigDataOverviewService.getOverviewTotalData(userJurisdiction.getOrganizationId(),day-1);

        return new ResultJsonUtil().success(resultData);
    }


    @ApiOperation(value = "获取各个网页的总浏览量")
    @GetMapping("/getPageVariousTotal")
    public ResultJsonUtil<Object> getPageVariousTotal(
            Authentication authentication,
            HttpServletRequest request) {
        UserJurisdiction  userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        List<JSONObject> listdata = bigDataAccessPageService.getPageVariousTotal(userJurisdiction.getOrganizationId());
        JSONObject data = new JSONObject();
        List<String> names = new ArrayList<>();
        List<Integer> pvCounts = new ArrayList<>();
        for(int i = 0;i<listdata.size();i++){
            names.add(i,listdata.get(i).getString("page_name"));
            pvCounts.add(i,listdata.get(i).getInteger("pv_count"));
        }
        data.put("names",names);
        data.put("values",pvCounts);
        return new ResultJsonUtil().success(data);
    }

    @ApiOperation(value = "获取近七天各个网页的浏览量")
    @GetMapping("/getPageVariousTotalBySevenDays")
    public ResultJsonUtil<Object> getPageVariousTotalBySevenDays(
            Authentication authentication,
            HttpServletRequest request) {
        UserJurisdiction  userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        List<JSONObject> listdata = bigDataAccessPageService.getPageVariousTotalBySevenDays(userJurisdiction.getOrganizationId());
        //近七天日期
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        for (int i = 6; i >= 0; i--) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }

        JSONObject reult = new JSONObject();
        List<JSONObject> data= new ArrayList<>();
        for(int i = 0;i<listdata.size();i++){
            JSONObject obj = new JSONObject();
            List<Integer> count= new LinkedList<>();
            count.add(listdata.get(i).getInteger("pv_6_count"));
            count.add(listdata.get(i).getInteger("pv_5_count"));
            count.add(listdata.get(i).getInteger("pv_4_count"));
            count.add(listdata.get(i).getInteger("pv_3_count"));
            count.add(listdata.get(i).getInteger("pv_2_count"));
            count.add(listdata.get(i).getInteger("pv_1_count"));
            count.add(listdata.get(i).getInteger("pv_0_count"));
            obj.put("name",listdata.get(i).getString("page_name"));
            obj.put("value",count);
            data.add(obj);
        }

        reult.put("dateList",dateList);
        reult.put("data",data);
        return new ResultJsonUtil().success(reult);
    }


    @ApiOperation(value = "获取仅网点历史全部浏览数据")
    @GetMapping("/getOverviewAllTotal")
    public ResultJsonUtil<Object> getOverviewAllTotal(
            Authentication authentication,
            HttpServletRequest request) {
        UserJurisdiction  userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        JSONObject resultData = bigDataOverviewService.getOverviewAllTotal(userJurisdiction.getOrganizationId());

        return new ResultJsonUtil().success(resultData);
    }



}
